/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PPersonInformation.java
 *
 * Created on Apr 5, 2012, 9:23:09 AM
 */
package com.bc5Neptune.cis.gui;

import com.bc5Neptune.cis.bll.ProcessImage;
import com.bc5Neptune.cis.entity.PersonEntity;
import com.bc5Neptune.cis.dal.PersonDAL;
import java.awt.image.BufferedImage;
import javax.swing.JOptionPane;
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

/**
 *
 * @author phu.huynh
 */
public class PPersonInformation extends javax.swing.JPanel {
    /* image to export to pdf */

    public BufferedImage bufferedImage;
    //public String pdfFileName;

    /** Creates new form PPersonInformation */
    public PPersonInformation() {
        initComponents();
    }

    public boolean exportToPDF() {
        boolean result = false;
        try {

            Document document = new Document();
            FileOutputStream fos = new FileOutputStream("../CIS_SProjectR2/" + txtFullName.getText() + ".pdf");
            PdfWriter writer = PdfWriter.getInstance(document, fos);
            document.open();
            ProcessImage proImg = new ProcessImage();
            Image image = Image.getInstance(proImg.bufferedImageToByteArray(bufferedImage));
          //  image.setAbsolutePosition(0, 600);
            
            document.add(image);
            Phrase space = new Phrase("\n\n----------------------------------------------------" 
                    + "\n", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL));
            document.add(space);
            
            Phrase identity = new Phrase("Indentity number: " 
                    + txtIdentity.getText() + "\n", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL));
            document.add(identity);
            
            Phrase fullname = new Phrase("Full name: " 
                    + txtFullName.getText() + "\n", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL));
            document.add(fullname);
            
            Phrase birthday = new Phrase("Birthday: " 
                    + txtBirthday.getText() + "\n", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL));
            document.add(birthday);
            
            Phrase hometown = new Phrase("Home Town: " 
                    + txtHomeTown.getText() + "\n", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL));
            document.add(hometown);
            
            Phrase residence = new Phrase("Place of residence: " 
                    + txtResidence.getText() + "\n", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL));
            document.add(residence);
            
            Phrase ethnic = new Phrase("Ethnic: " 
                    + txtEthnic.getName() + "\n", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL));
            document.add(ethnic);
            
            Phrase religious = new Phrase("Religious: " 
                    + txtReligious.getText() + "\n", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL));
            document.add(religious);
            
            document.close();
            result = true;
            System.out.println("File is created successfully showing header and footer.");
        } catch (Exception ex) {
            result = false;
            System.out.println(ex);

        }
        return result;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane2 = new javax.swing.JSplitPane();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        scrollPanel = new javax.swing.JScrollPane();
        listNearestPerson = new javax.swing.JList();
        pnlInformation = new javax.swing.JPanel();
        pnlShowInfor = new javax.swing.JPanel();
        jPanel19 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtIdentity = new javax.swing.JTextField();
        txtFullName = new javax.swing.JTextField();
        txtBirthday = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane9 = new javax.swing.JScrollPane();
        txtHomeTown = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane8 = new javax.swing.JScrollPane();
        txtResidence = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        txtEthnic = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtReligious = new javax.swing.JTextField();
        jPanel18 = new javax.swing.JPanel();
        lblImage = new javax.swing.JLabel();
        jButton7 = new javax.swing.JButton();

        jSplitPane1.setDividerLocation(150);
        jSplitPane1.setDividerSize(2);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        scrollPanel.setViewportView(listNearestPerson);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 615, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
        );

        jSplitPane1.setTopComponent(jPanel1);

        pnlShowInfor.setBackground(java.awt.Color.gray);
        pnlShowInfor.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Person Imformation ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.white));

        jPanel19.setBackground(java.awt.Color.gray);

        jLabel1.setFont(new java.awt.Font("Cantarell", 0, 15));
        jLabel1.setForeground(java.awt.Color.white);
        jLabel1.setText("Identidy number:");

        txtIdentity.setBackground(java.awt.Color.lightGray);
        txtIdentity.setEditable(false);

        txtFullName.setBackground(java.awt.Color.lightGray);
        txtFullName.setEditable(false);

        txtBirthday.setBackground(java.awt.Color.lightGray);
        txtBirthday.setEditable(false);

        jLabel3.setFont(new java.awt.Font("Cantarell", 0, 15));
        jLabel3.setForeground(java.awt.Color.white);
        jLabel3.setText("Birthday:");

        jLabel2.setFont(new java.awt.Font("Cantarell", 0, 15));
        jLabel2.setForeground(java.awt.Color.white);
        jLabel2.setText("Full name:");

        jLabel4.setFont(new java.awt.Font("Cantarell", 0, 15));
        jLabel4.setForeground(java.awt.Color.white);
        jLabel4.setText("Hometown:");

        txtHomeTown.setColumns(10);
        txtHomeTown.setEditable(false);
        txtHomeTown.setRows(3);
        txtHomeTown.setTabSize(5);
        jScrollPane9.setViewportView(txtHomeTown);

        jLabel5.setFont(new java.awt.Font("Cantarell", 0, 15));
        jLabel5.setForeground(java.awt.Color.white);
        jLabel5.setText("Place of residence:");

        txtResidence.setColumns(10);
        txtResidence.setEditable(false);
        txtResidence.setRows(3);
        txtResidence.setTabSize(5);
        jScrollPane8.setViewportView(txtResidence);

        jLabel6.setFont(new java.awt.Font("Cantarell", 0, 15));
        jLabel6.setForeground(java.awt.Color.white);
        jLabel6.setText("Ethnic:");

        txtEthnic.setBackground(java.awt.Color.lightGray);
        txtEthnic.setEditable(false);

        jLabel7.setFont(new java.awt.Font("Cantarell", 0, 15));
        jLabel7.setForeground(java.awt.Color.white);
        jLabel7.setText("Religious:");

        txtReligious.setBackground(java.awt.Color.lightGray);
        txtReligious.setEditable(false);

        javax.swing.GroupLayout jPanel19Layout = new javax.swing.GroupLayout(jPanel19);
        jPanel19.setLayout(jPanel19Layout);
        jPanel19Layout.setHorizontalGroup(
            jPanel19Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1)
            .addComponent(jLabel2)
            .addComponent(jLabel3)
            .addComponent(jScrollPane9, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
            .addComponent(jLabel4)
            .addComponent(jLabel5)
            .addComponent(jScrollPane8, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
            .addComponent(jLabel6)
            .addComponent(txtEthnic, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
            .addComponent(jLabel7)
            .addComponent(txtBirthday, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
            .addComponent(txtFullName, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
            .addComponent(txtIdentity, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
            .addComponent(txtReligious, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
        );
        jPanel19Layout.setVerticalGroup(
            jPanel19Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel19Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtIdentity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtBirthday, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtEthnic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtReligious, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel18.setBackground(java.awt.Color.gray);
        jPanel18.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Original Image", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.white));

        javax.swing.GroupLayout jPanel18Layout = new javax.swing.GroupLayout(jPanel18);
        jPanel18.setLayout(jPanel18Layout);
        jPanel18Layout.setHorizontalGroup(
            jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel18Layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(lblImage, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(6, Short.MAX_VALUE))
        );
        jPanel18Layout.setVerticalGroup(
            jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel18Layout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addComponent(lblImage, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/must_have_icon_set/Print/Print_16x16.png"))); // NOI18N
        jButton7.setText("   Export to PDF  ");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlShowInforLayout = new javax.swing.GroupLayout(pnlShowInfor);
        pnlShowInfor.setLayout(pnlShowInforLayout);
        pnlShowInforLayout.setHorizontalGroup(
            pnlShowInforLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlShowInforLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlShowInforLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel18, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton7))
                .addGap(18, 18, 18)
                .addComponent(jPanel19, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlShowInforLayout.setVerticalGroup(
            pnlShowInforLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlShowInforLayout.createSequentialGroup()
                .addGroup(pnlShowInforLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlShowInforLayout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(jPanel18, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton7))
                    .addComponent(jPanel19, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(55, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout pnlInformationLayout = new javax.swing.GroupLayout(pnlInformation);
        pnlInformation.setLayout(pnlInformationLayout);
        pnlInformationLayout.setHorizontalGroup(
            pnlInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 615, Short.MAX_VALUE)
            .addGroup(pnlInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(pnlShowInfor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlInformationLayout.setVerticalGroup(
            pnlInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 523, Short.MAX_VALUE)
            .addGroup(pnlInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(pnlShowInfor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jSplitPane1.setRightComponent(pnlInformation);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 615, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 675, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
        if (exportToPDF()) {
            File file = new File(txtFullName.getText() +".pdf");
            JOptionPane.showMessageDialog(this, "Export to PDF sucessfuly\n" + file.getAbsolutePath(), "Information", 1, null);
        } else {
            JOptionPane.showMessageDialog(this, "Error when export to PDF please try again", "Information", 1, null);
        }

    }//GEN-LAST:event_jButton7ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel18;
    private javax.swing.JPanel jPanel19;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JScrollPane jScrollPane9;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    public javax.swing.JLabel lblImage;
    public javax.swing.JList listNearestPerson;
    public javax.swing.JPanel pnlInformation;
    public javax.swing.JPanel pnlShowInfor;
    public javax.swing.JScrollPane scrollPanel;
    public javax.swing.JTextField txtBirthday;
    public javax.swing.JTextField txtEthnic;
    public javax.swing.JTextField txtFullName;
    public javax.swing.JTextArea txtHomeTown;
    public javax.swing.JTextField txtIdentity;
    public javax.swing.JTextField txtReligious;
    public javax.swing.JTextArea txtResidence;
    // End of variables declaration//GEN-END:variables
}
